package com.jd.controller;

import com.jd.entity.AddressEntity;
import com.jd.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/addressController")
public class AddressController {

    @Autowired
    AddressService addressService;

    @RequestMapping(value="/list")
    public List<AddressEntity> list(HttpServletRequest request) {
        String userId = request.getSession().getAttribute("userId").toString();
        return addressService.list(userId);
    }

    @RequestMapping(value="/getById")
    public AddressEntity getById(String id) {
        return addressService.getById(id);
    }

    @RequestMapping(value="/del")
    public void del(String id) {
        addressService.delById(id);
    }

    @RequestMapping(value="/save")
    public void save(@RequestBody AddressEntity addressEntity,HttpServletRequest request) {
        String userId = request.getSession().getAttribute("userId").toString();
        addressEntity.setUserId(userId);
        addressEntity.setCreateTime(new Date());
        addressService.save(addressEntity);
    }
}
